package com.manatee.adnet;
import java.io.*;
import java.net.URL;

import android.support.v7.app.ActionBarActivity;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.view.View;
import android.widget.*;

import org.json.JSONException;

import android.widget.Button;

import com.example.commercailapp.R;
import com.manatee.commercailapp.MainActivity;


public class AdNet extends Activity{
	
	
	private static AdNet instance = null;
	protected AdNet() {
	      // Exists only to defeat instantiation.
	}
	public static AdNet getInstance() {
	   if(instance == null) {
	      instance = new AdNet();
	   }
	   return instance;
	}
	
	public String jsonCommercials;
	private AdNetInterface callback=null; 
	private Button btn_getCommercials;
	private TextView txt_debug;
	private String url;
	private MainActivity anf;
	
	public void setMainActivity (MainActivity ma) {
		anf = ma;
	}
	
	
	public void getCommercials(){
		try {
			url = "http://adnetwork.kristoffermads.dk/get-commercial/12/12";
			
			Thread t = new Thread(new Runnable() {
			     public void run()
			     {
			    	 JsonHelper jh = new JsonHelper();

					 try {
						 
						jsonCommercials = jh.readJsonFromUrl(url);
						Log.i("Mess", "Download complete calling callback");
						callback.getCommercialSuccess(getInstance());
						
					} catch (Exception e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
				
			     }}});  t.start();
			
		} catch (Exception e) {
			String message = e.getMessage();
//	    	if (this.callback!=null)
//	    		this.callback.getCommercialFailed(this);
		} 

	}
	
//	public void getCommercialsNull()
//	{
//		setCallback(null);
//		this.getCommercials();
//	}
	
	
	//Sets the callback which all method should call
	public void setCallback (AdNetInterface anif) {
		this.callback = anif;
	}
//	
	//show commercials in android view on the screen
	//uses the callback (interface) for reporting results
	public void showCommercials() {
		
		//show view
	   if (jsonCommercials == null & this.callback == null) {
		   
		   this.setCallback( new AdnetShowCommercialHelper() );
		   
		   //TODO use inline function
		   
		   Log.d("Mess", "no commercial downloaded setting callback") ;
		   
	   }else{
		   Log.d("Mess", "show commercial pop up") ;
		   
		  //showCommercials;
		   if (anf != null & jsonCommercials != null)
		   {
			   anf.runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					// TODO Auto-generated method stub
					//display contents in new a window
					
					ViewHelper vh = new ViewHelper(anf);
					//vh.displayTextView(jsonCommercials);
					vh.diplayWindowView(jsonCommercials);

				}
			});
			   
		   }
			   
		   
	   }
	   
	   
		

	}
	
	private class AdnetShowCommercialHelper implements AdNetInterface{

		@Override
		public void getCommercialFailed(AdNet an) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void getCommercialSuccess(AdNet an) {
			an.showCommercials();
			// TODO Auto-generated method stub
			
		}
		
	}
	
//	btn_getCommercials = (Button) anif.findViewById(R.id.getCommercials);
//	txt_debug = (TextView) anif.findViewById(R.id.debugText);
//	
//	btn_getCommercials.setOnClickListener(new View.OnClickListener() {
//        public void onClick(View v) {
//        	if (jsonCommercials != null)
//        		txt_debug.setText(jsonCommercials);
//        	Log.i("TextViewData", jsonCommercials);
//        }
//    });
	
//    
//	// 0 = landscape, 1 = portrait
//	public void setOrientation () {
//		
//	}
//	
//	//Reports true if a commercial is ready to be shown immediately, false if not.
//	public boolean canShowCommercial () {
//		return false;
//	}
	
	
	
//	//Registers the client with the REST API
//	public void startSession () {
//		
//	}
//	
//	//Ends the session with the REST API
//	public void endSession () {
//		
//	}
//	
//	//Tells the REST API that we have rested our app.
//	public void pauseSession () {
//		
//	}
//	
//	//Tells the REST API that we have resumed our app.
//	public void resumeSession () {
//		
//	}
	

	
	
    
    
    
    
  
}


